<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>说说界面</title>
</head>
<link rel="stylesheet" href="css/myTalk.css">
<link rel="stylesheet" href="css/myTalk.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
<body>
<div id="cover"><!-- 遮布 -->
                    <div class="card-header" id="header">
                        <div class="row" id="rowes">

                            <div class="col-sm-4 d-flex justify-content-center" id="colus">
                                <p id="useress">用户:{{username}}</p>
                            </div>

                            <div id="saysay">
                                <button id="sayes" type="button"  data-toggle="modal" data-target="#saysomething">发表说说</button>
                            </div>

                            <div id="colout"><button id="coout" type="button" onclick="logout()">退出账号</button>
                            </div>

                        </div>
                    </div>
            <div class="col-sm"></div>

        <div class="row">
            <div class="col-sm">
            </div>
            <div id="saysayMsg">
                {% for msg in msgs %}
                <div id="cardess">
                    <div>
                        <div class="row">
                            <div class="col-sm-10">{{msg.content}}</div>
                            <div class="col-sm-2">
                                <button type="button" id="msgs" data-toggle="modal" data-target="#comment"
                                    onclick="msgCommit('{{msg.id}}')">评论/回复
                                </button>
                            </div>
                        </div>

                    </div>
                    <div class="card-body" id="cardes">
                        <ul id="{{msg.id}}">
                            {% for cmot in msg.comments %}
                            <li >
                                <blockquote class="blockquote mb-0">
                                    <p>{{cmot.content}}</p>
                                    <footer class="blockquote-footer">
                                        <cite title="Source Title">{{cmot.comUserName}}</cite>&nbsp;&nbsp;对
                                        <cite title="Source Title">{{msg.msgUserName}}</cite>&nbsp;&nbsp;说
                                    </footer>
                                </blockquote>
                            </li>
                            {% endfor %}
                        </ul>

                    </div>
                </div>

                {% endfor %}

            </div>
            <div class="col-sm">
            </div>
        </div>
    </div>


    <!-- 发表说说  -->
    <div class="modal fade" id="saysomething" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">发表说说</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form>
                        <div class="form-group">
                            <input type="text" class="form-control" aria-describedby="passwordHelpInline" name="msgSay"
                                id="msgSay">
                            <!-- <small id="passwordHelpInline" class="text-muted">
                            Must be 8-20 characters long.
                          </small> -->
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="say()">发表</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 评论回复 -->
    <div class="modal fade" id="comment" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">回复/评论</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form>
                        <div class="form-group">
                            <input type="text" class="form-control" aria-describedby="passwordHelpInline"
                                id="commentText">
                        </div>
                    </form>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" data-dismiss="modal"
                        onclick="saveComment()">评论/回复</button>
                </div>

            </div>
        </div>
    </div>
</div>

</body>
<script src="js/jquery-3.6.0.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>
    //注销
    let currentMsgId = '';
        function logout() {
            $.ajax({
                url: "/logout",
                type: "POST",
                dataType: "json",
                success: function (res) {
                    if (res.code === 200) {
                        window.location.href = "/";
                    }
                },
                error: function (msg) {
                    console.log('网络或者服务器有问题');
                }
            })
        }

    //发表说说
        function say() {
            let data = {
                msgSay: $('#msgSay').val()
            }
            $.ajax({
                url: "/say",
                type: "POST",
                dataType: "json",
                data: data,
                success: function (res) {
                    if (res.code === 200) {
                        let data = JSON.parse(res.data);
                        console.log(data);
                        let html = `
                        <div class="card border-primary shadow" style="width: 50rem;">
                            <div class="card-header">
                                <div class="row">
                                    <div class="col-sm-10">${data.content}</div>
                                    <div class="col-sm-2"><button type="button" class="btn btn-secondary btn-sm"
                                            data-toggle="modal" data-target="#comment"
                                            onclick="msgCommit(${data.id})">评论/回复</button>
                                    </div>
                                </div>
                            </div>
                            <div class="card-body">
                                <ul class="list-group list-group-flush" id="${data.id}">
                    
                                </ul>
                            </div>
                        </div>
                        `;
                        $("#saysayMsg").prepend(html);
                    }
                },
                error: function (msg) {
                    console.log('网络或者服务器有问题');
                }
            })
        }
        function msgCommit(msgId) {
            currentMsgId = msgId;
        }
        function saveComment() {

let data = {
    msgId: currentMsgId,
    content: $('#commentText').val()
}
$.ajax({
    url: "/comment",
    type: "POST",
    dataType: "json",
    data: data,
    success: function (res) {

        if (res.code === 200) {
            let data=res.data;
            console.log(data);
            let html=`
                <li class="list-group-item">
                    <blockquote class="blockquote mb-0">
                        <p>${data.content}</p>
                        <footer class="blockquote-footer">
                            <cite title="Source Title">${data.fromUserName}</cite>&nbsp;&nbsp;对
                            <cite title="Source Title">${data.toUserName}</cite>&nbsp;&nbsp;说
                        </footer>
                    </blockquote>
                </li>
            `;
            $("#" + currentMsgId).append(html);
        }
    },
    error: function (msg) {
        console.log('网络或者服务器有问题');
    }
});
}
</script>
</html>